package com.ruoyi.common.Selfadd;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author LiYiFan
 * @dese 如何使用mysql实现定时添加日期对象和数据
 * @time 2024-04-24
 */

@Component
public class DatabaseUtil {


    @Autowired
    private JdbcTemplate jdbctemplate;

    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行
    public void addDateToDatabase(){
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate  = sdf.format(currentDate);
        jdbctemplate.update("insert into ribao(briefing_cycle) values(?)",formattedDate);
        System.out.println("添加成功");
    }
}